import BaseSprite from "../../../Script/Base/BaseSprite";
import { EResType } from "../../../Script/Common/Enum";
import { GlobalData } from "../../../Script/Common/GlobalData";
import { ISayConfig, TalkModule } from "../../../Script/Common/TalkModule";
import GameUtil from "../../../Script/Util/GameUtil";
import { LoadUtil } from "../../../Script/Util/LoadUtil";
import { IBaseSkeData } from "../../Interface/IMessage";
import { SceneGame } from "../../SceneGame";
import AudioManager from "../../Util/AudioManager";
import StorageUtil from "../../Util/StorageUtil";

const { ccclass, property } = cc._decorator;

@ccclass
export default class BubbleUI extends BaseSprite {

    @property({ type: cc.Node, tooltip: "头像节点" })
    public iconNode: cc.Node = null;
    @property({ type: cc.Sprite, tooltip: "头像" })
    public icon: cc.Sprite = null;

    @property({ type: cc.Label, tooltip: "对话内容" })
    public lab: cc.Label = null;

    private config: ISayConfig = null;

    public async refresh(config: ISayConfig, callback: Function) {
        this.config = config;
        // 设置头像及位置
        this.iconNode.x = config.name === '顾云泽' ? 195 : -195;
        LoadUtil.instance.setSpriteFrame(this.icon, EResType.BubbleIcon, config.name);

        if (config?.audio) {
            AudioManager.instance.playSound(config.audio);
        }

        this.node.active = true;
        GameUtil.effectWords(this.lab, config.desc, async () => {
            await GameUtil.sleep(GlobalData.isFast ? 100 : 1000);
            config.isComplete = true;
            this.node.active = false;

            // 前情用nextId、正常不需要nextId(用nextId容易播两次)
            const talkData = StorageUtil.instance.getTalkData();
            if (talkData.id < 0) {
                if (config.nextId) {
                    const nextData = TalkModule.getSayConfigById(config.nextId);
                    this.refresh(nextData, callback);
                }
            }
            await GameUtil.sleep(500);
            callback && callback(config.id);
            callback = null;

            // 存数据(播下条对话)
            TalkModule.saveTalkData();
        });
    }
}